<?php
class Category extends Action {
	/* (non-PHPdoc)
	 * @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$data = array ();
		
		$cateModel = new WebPageCategory ();
		
		$items = $cateModel->sortAsc ( 'up_cid' )->sortAsc ( 'cid' )->retrieve ();
		
		if ($items) {
			$data ['items'] = $this->getSubCates ( $items );
		}
		
		$data ['display'] = array (0 => '<span class="display label">隐藏</span>', '1' => '<span class="display label label-success">显示</span>' );
		$data ['navi'] = 'category';
		$data ['cate_types'] = apply_filter ( "get_category_types", array () );
		bind ( 'get_category_operation', array ($this, 'get_category_operation' ), 10, 2 );
		return new SmartyView ( $data, 'vfs/Category' );
	}
	public function get_category_operation($operation, $item) {
		
		$operation .= '<a title="添加子栏目" href="./?Ctlr=AddCategory&up_cid=' . $item ['cid'] . '&up_cname=' . urlencode ( $item ['title'] ) . '"><i class="icon-plus"></i></a>';
		
		$operation .= '<a title="编辑" href="./?Ctlr=EditCategory&cid=' . $item ['cid'] . '"><i class="icon-edit"></i></a>';
		
		$operation .= '<a title="删除" onclick="return confirm(\'确定要删除该栏目?\');" href="./?Ctlr=DelCategory&cid=' . $item ['cid'] . '"><i class="icon-trash"></i></a>';
		
		if ($item ['display']) {
			$operation .= '<a title="点击设为导航不可见" href="#"><i class="display icon-eye-open"></i></a>';
		} else {
			$operation .= '<a title="点击设为导航可见" href="#"><i class="display icon-eye-close"></i></a>';
		}
		
		if ($item ['page_id']) {
			$operation .= '<a data-content="' . $item ['title'] . '" id="pcate-' . $item ['cid'] . '" title="编辑栏目对应页面" href="' . ADMIN_URL . 'vfs/?Ctlr=Page&pid=' . $item ['page_id'] . '" class="publish"><i class="icon-file"></i></a>';
		} else {
			$operation .= '<a title="发布此栏目" href="./?Ctlr=Page&cmode=category&ctype=category&category=' . $item ['name'] . '" class="npublish"><i class="icon-globe"></i></a>';
		}
		
		return $operation;
	}
	private function getSubCates($items) {
		$cates = array ();
		foreach ( $items as $item ) {
			if ($item ['up_cid'] == 0) { //顶级栏目
				$item ['tree_id'] = 'c-' . $item ['cid'];
			} else {
				$item ['tree_id'] = 'c-' . $item ['cid'];
				$item ['tree_clz'] = 'hide item-of-c-' . $item ['up_cid'];
			}
			$cates [] = $item;
		}
		return $cates;
	}
}